<?php
class RP_Model_User_Base
{
	private $user_id;
	
	protected $_user_type_id;
	
	private $username;
	
	private $password;
	
	private $email;
	
	private $valid_from;
	
	const GUEST = 'guest';
	
	/**
	 * @return the $user_id
	 */
	public function getUser_id() {
		if(!isset($this->user_id)){
			self::_fetchUserId();
		}
		return $this->user_id;
	}
	/**
	 * @return the $user_type_id
	 */
	public function getUser_type_id() {
		return $this->_user_type_id;
	}
	/**
	 * @return the $username
	 */
	public function getUsername() {
		return $this->username;
	}

	/**
	 * @return the $password
	 */
	public function getPassword() {
		return $this->password;
	}

	/**
	 * @return the $email
	 */
	public function getEmail() {
		return $this->email;
	}
	
	/**
	 * @return the $valid_from
	 */
	public function getValid_from() {
		return $this->valid_from;
	}
	
	/**
	 * @param field_type $user_id
	 */
	public function setUser_id($user_id) {
		$this->user_id = $user_id;
		return $this;
	}
	/**
	 * @param the $user_type_id
	 */
	public function setUser_type_id($user_type_id) {
		$this->_user_type_id = $user_type_id;
		return $this;
	}
	
	/**
	 * @param field_type $username
	 */
	public function setUsername($username) {
		$this->username = $username;
		return $this;
	}

	/**
	 * @param field_type $password
	 */
	public function setPassword($password) {
		$this->password = $password;
		return $this;
	}

	/**
	 * @param field_type $email
	 */
	public function setEmail($email) {
		$this->email = $email;
		return $this;
	}
	
	/**
	 * @param field_type $valid_from
	 */
	public function setValid_from($valid_from) {
		$this->valid_from = $valid_from;
		return $this;
	}
	
	
	public function __construct (array $options = null)
    {
        if (is_array($options)) {
            $this->setOptions($options);
        }
        
    }
    public function setOptions (array $options)
    {
        $methods = get_class_methods($this);
        foreach ($options as $key => $value) {
            $method = 'set' . ucfirst($key);
            if (in_array($method, $methods)) {
                $this->$method($value);
            }
        }
        return $this;
    }
    
	protected function _fetchUserId ()
    {
        if (is_null(self::getUsername())) {
            throw new Exception('Username is null.', Zend_Log::ERR);
        }
        $dbTable = new RP_Model_DbTable_AuthUser();
        
        $id = $dbTable->select()
        		->from('auth_user','user_id')
        		->where('username = ?',$this->getUsername())
        		->query()
        		->fetchColumn();
        if (! $id) {
            throw new Exception(
            'Unable to retrieve user_id from username "' .
             $this->getUsername() . '"', Zend_Log::ERR);
        }
        $this->setUser_id($id);
        return;
    }
    
    /**
     * 
     * function populateUser
     * @param $purpose defines how to populate the object.
     * It can take many values e.g. $purpose = 'view' means populate user from normal view. Required for the application.
     * 								$purpose = 'recover' means populate user from email and user_type_id. Required in case of user forgot credentails.	
     * Use to fill the complete object.
     */
    public function populateUser($purpose = 'normal'){
    	$result = null;
    	$cols = array('user_id','user_type_id','username','email','valid_from','password');
    	if($purpose == 'normal'){
    	$result = Zend_Db_Table::getDefaultAdapter()
    				->select()
    				->from('auth_user',$cols)
    				->where('user_id = ?',$this->getUser_id())
    				->query()->fetch();
    	}
    	else if($purpose == 'recover'){
    		$result = Zend_Db_Table::getDefaultAdapter()
    				->select()
    				->from('auth_user',$cols)
    				->where('email = ?',$this->getEmail())
    				->where('user_type_id = ?',$this->getUser_type_id())
    				->query()->fetch();
    	}
    	
    	if($result!=null){
    		$this->setUser_type_id($result['user_type_id'])
    			 ->setUsername($result['username'])
    			 ->setEmail($result['email'])
    			 ->setValid_from($result['valid_from'])
    			 ->setPassword($result['password']);
    		return true;
    	}
    	return false;
    }
    
    	
	public function isUsernameAvailable(){
    	$sql = "SELECT username FROM auth_user WHERE username = ?";
    	$bind = array($this->username);
	 	
	 	$dbTable = new RP_Model_DbTable_AuthUser();
	 	$result = $dbTable->getDefaultAdapter()->query($sql,$bind)->fetch();
    	
    	if($result["username"] == $this->username){
    		return false;
    	}
    	return true;
    }
    
    public function isEmailAvailable(){
    	$sql = 'SELECT email, user_type_id FROM auth_user WHERE email = ? AND user_type_id = ?';
    	$bind = array($this->email, $this->getUser_type_id()); 
    	
    	$dbTable = new RP_Model_DbTable_AuthUser();
	 	$result = $dbTable->getDefaultAdapter()->query($sql,$bind)->fetch();
	 	
	 	if(($result["email"] == $this->email) && ($result["user_type_id"] == $this->_user_type_id)){
    		return false;
    	}
    	return true;
    }
    
}
?>